package GUI; //done

import java.io.*; 
import java.util.Date;
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

/**
 * This Class controls the Report Window.
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public class ReportWindow extends JPanel implements ActionListener {
	
	/** The serial Version UID. */
	private static final long serialVersionUID = 1L;

	/** The new line Command. */
	static private final String newline = "\n"; 
	
	/** The Open and Save Buttons. */
	private JButton openButton, saveButton;
	
	/** The Report log Text Area. */
	private JTextArea log;
	
	/** The File Chooser. */
	private JFileChooser fc;
	
	/**
	 * The default constructor
	 */
	public ReportWindow() { 
		super(new BorderLayout()); 
		log = new JTextArea(5,20); 
		log.setMargin(new Insets(5,5,5,5)); 
		JScrollPane logScrollPane = new JScrollPane(log);
		
		//Create a file chooser 
		fc = new JFileChooser(); 
		openButton = new JButton("Open a File...", createImageIcon("images/Open16.gif")); 
		openButton.setBackground(SystemColor.scrollbar);
		openButton.addActionListener(this); 
		//Create the save button. We use the image from the JLF 
		//Graphics Repository (but we extracted it from the jar). 
		saveButton = new JButton("Save a File...", createImageIcon("images/Save16.gif")); 
		saveButton.setBackground(SystemColor.scrollbar);
		saveButton.addActionListener(this); 
		//For layout purposes, put the buttons in a separate panel 
		JPanel buttonPanel = new JPanel(); //use FlowLayout 
		buttonPanel.setBackground(SystemColor.activeCaption);
		buttonPanel.add(openButton);
		buttonPanel.add(saveButton);
		//Add the buttons and the log to this panel. 
		add(buttonPanel, BorderLayout.PAGE_START); 
		add(logScrollPane, BorderLayout.CENTER); 
	}
	
	/**
	 * This function sets the showed text on the report.
	 */
	public void setText(String str){
		Date date = new Date();
		log.setText(date.toString()+"\n"+"CCRM Report:\n"+str);
	}
	
	/**
	 * The action performed when the report is opened.
	 */
	public void actionPerformed(ActionEvent e){ 
		if (e.getSource() == openButton){ 
			int returnVal = fc.showOpenDialog(ReportWindow.this); 
			if (returnVal == JFileChooser.APPROVE_OPTION){ 		
			} 
			else{ 
				log.append("Open command cancelled by user." + newline); 
			}
			log.setCaretPosition(log.getDocument().getLength());
		//Handle save button action. 
		} 
		else if (e.getSource() == saveButton) { 
			int returnVal = fc.showSaveDialog(ReportWindow.this); 
			if (returnVal == JFileChooser.APPROVE_OPTION) { 		
				File file = fc.getSelectedFile(); 
				System.out.println(fc.getSelectedFile());
				try{
					FileOutputStream fs = new FileOutputStream(fc.getSelectedFile()+".doc");
					OutputStreamWriter out = new OutputStreamWriter(fs);            
					out.write(log.getText());
					out.close();
				}
				catch (IOException e1){
					System.err.println(e1);
				}
				//This is where a real application would save the file. 
				log.append("Saving: " + file.getName() + "." + newline); 
			} 
			else { 
				log.append("Save command cancelled by user." + newline); 
			}
		log.setCaretPosition(log.getDocument().getLength());
		}
	}
	
	/**
	 * This function creates an image icon.
	 * Returns an ImageIcon, or null if the path was invalid. 
	 */ 
	protected static ImageIcon createImageIcon(String path) { 
		java.net.URL imgURL = ReportWindow.class.getResource(path); 
		if (imgURL != null) { 
			return new ImageIcon(imgURL); 
		} 
		else { 
			System.err.println("Couldn't find file: " + path); 
			return null; 
		}
	}
}